package a66weding.com.jiehuntong.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashMap;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.application.DataWareHourse;
import a66weding.com.jiehuntong.application.GetUserInfo;
import a66weding.com.jiehuntong.application.GlobalApplication;
import a66weding.com.jiehuntong.beans.CommonResultBean;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.common.utils.CUtils;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import a66weding.com.jiehuntong.http.UrlDealUtil;

/**
 * 我的-设置-真实姓名界面
 */
public class MeUserRealnameActivity extends MeBaseActivity implements View.OnClickListener , CompoundButton.OnCheckedChangeListener, OnNetResultListener {
    private LinearLayout mBackLay;
    private TextView mBackContentTv;
    private TextView mHeaderTitleTv;
    private RelativeLayout mHeaderRightRlay;
    private TextView mHintInfoTv;
    private Button mCommitNoBtn;
    private EditText mContentEt;
    private int intentTo;
    private String realName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_me_user_nickname);
        ((GlobalApplication)getApplication()).getActivityManager().addActivity("MeUserRealnameActivity",this);
        Intent intent = getIntent();
        if(intent!=null) {
            intentTo = intent.getIntExtra(MeBaseActivity.INTENTTO, 0);
        }

        initview();
    }

    /**
     * 显示内容调用的方法
     */
    @Override
    protected void onResume() {
        super.onResume();
        mContentEt.setText(DataWareHourse.getInstance().getmUser().getData().get(0).getNvc_RealName());
    }

    private void initview() {
        mBackLay = (LinearLayout) findViewById(R.id.llay_header_back);
        mBackContentTv = (TextView) findViewById(R.id.tv_header_backcontent);
        mHeaderTitleTv = (TextView) findViewById(R.id.tv_header_title);
        mHeaderRightRlay = (RelativeLayout) findViewById(R.id.rlay_header_rightfunc);
        mHintInfoTv = (TextView) findViewById(R.id.tv_me_sethint);
        mCommitNoBtn = (Button) findViewById(R.id.btn_editcommit_no);
        mContentEt = (EditText) findViewById(R.id.et_me_set_content);

        mBackContentTv.setText("返回");
        mHeaderTitleTv.setText("填写昵称");

        realName = mContentEt.getText().toString().trim();

        mHeaderRightRlay.setVisibility(View.GONE);

        if(intentTo==MeBaseActivity.REALNAME) {
            mHeaderTitleTv.setText("填写姓名");
            mHintInfoTv.setText("请填写您的姓名");
        }
        mContentEt.setText(GetUserInfo.getDefault().getRealName()); // 在修改姓名之前先得到之前设置的姓名
        mBackLay.setOnClickListener(this);
        mCommitNoBtn.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
                case R.id.llay_header_back:
                    finish();
                break;
                 case R.id.btn_editcommit_no:
                     realName = mContentEt.getText().toString();
                         if(TextUtils.isEmpty(realName)) {
                             CUtils.showToast("姓名不能为空");
                             return;
                         } else if (realName.length() > 15){
                             CUtils.showToast("姓名过长");
                             return;
                         } else if (isConSpeCharacters(realName)){
                             CUtils.showToast("姓名不能含有特殊字符");
                             return;
                         } else if (realName.contains(" ")){
                             CUtils.showToast("姓名不能含有空格");
                             return;
                         }
                         String url= UrlDealUtil.getRealUrl(RequestCode.UPDATE_REALNAME);
                         HashMap<String,String> requestParams=new HashMap<String,String>();
                         requestParams.put("RealName",""+realName);
                         HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST, url,
                                 this, CommonResultBean.class, RequestCode.UPDATE_REALNAME, requestParams);
                         HttpHandleUtil.executeCommonPost(this,httpRequestBean);

                break;
        }
    }

    //string 为要判断的字符串
    public static boolean isConSpeCharacters(String string){
        if(string.replaceAll("[\u4e00-\u9fa5]*[a-z]*[A-Z]*\\d*-*_*\\s*","").length()==0){
            //不包含特殊字符
            return false;
        }
        return true;
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    }

    @Override
    public void onSucess(SuccessBean sucessbean) {
        //更新内存
        GetUserInfo.getDefault().setNickName(realName); // 网络请求成功之后,向自己存放数据的类中设置昵称
        //更新本地缓存
        DataWareHourse.getInstance().getmUser().getData().get(0).setNvc_RealName(realName);
        Toast.makeText(this,"姓名设置成功",Toast.LENGTH_SHORT).show();
        finish();
    }

    @Override
    public void onError(ErrorBean errorBean) {
        Toast.makeText(this,"数据请求失败",Toast.LENGTH_SHORT).show();
    }
}
